from flask_restful import Resource, reqparse,request
from ....Model.Models import User as UserModel
from ....utils import timestamp,response
from flask_restful_swagger import swagger


success = 1
notfond = -1
error = 0

examin_is = 1
examin_no = 0
# user_poot_parser = reqparse.RequestParser()
# user_poot_parser.add_argument('account', type=str)
# user_poot_parser.add_argument('password', type=str)


class Login(Resource):
    def __init__(self, db, auth=None):
        self.db = db

    @swagger.operation(
      notes='登录',
      parameters=[
            {
            "name": "phone",
            "description": "手机号",
            "dataType": 'string',
            "paramType": "form"
          },{
            "name": "password",
            "description": "密码",
            "dataType": 'string',
            "paramType": "form"
          }
      ],
      responseMessages=[
          {
              "code": 201,
              "message": "Created. The URL of the created blueprint should " +
              "be in the Location header"
          },
          {
              "code": 405,
              "message": "Invalid input"
          }
      ])
    def post(self):#登录
        try:
            args = request.form #获取post请求中的form数据
            #从数据库查询当前手机号的记录并转换为user类对象
            user_obj =  self.db.session.query(UserModel).filter_by(phone=args['phone']).first() 
            #判断对象是否存在
            if(user_obj != None):
                #验证form数据里密码是否正确
                if(user_obj.verify_password(args['password'])):
                    #吧当前的时间戳存入到数据库中
                    user_obj.last_login = timestamp()
                    #设置user的token值
                    user_obj.set_token()
                    return response(success,None, user_obj.as_dict())
                else:
                    return response(error,"账号密码错误",None)
            else:
                return response(error,"账号未注册",None)
        except Exception as er:
            return response(error,er,None)




class Register(Resource):
    def __init__(self, db, auth=None):
        self.db = db


    @swagger.operation(
      notes='注册账号',
      parameters=[
          {
            "name": "name",
            "description": "名字",
            "dataType": 'string',
            "paramType": "form"
          },
          {
            "name": "head_picture",
            "description": "头像",
            "dataType": 'string',
            "paramType": "form"
          },{
            "name": "phone",
            "description": "手机号",
            "dataType": 'string',
            "paramType": "form"
          },{
            "name": "password",
            "description": "密码",
            "dataType": 'string',
            "paramType": "form"
          }
      ],
      responseMessages=[
          {
              "code": 201,
              "message": "Created. The URL of the created blueprint should " +
              "be in the Location header"
          },
          {
              "code": 405,
              "message": "Invalid input"
          }
      ])
    def post(self):#添加
        try:
            args = request.form #从post请求中获取form数据
            #判断当前添加的手机号是否已经存在
            if(self.db.session.query(UserModel).filter_by(phone=args['phone']).first()==None):
                try:
                    #将数据存入数据库
                    self.db.session.add(
                            UserModel(
                                name=args['name'],
                                head_picture=args['head_picture'],
                                phone =args['phone'],
                                examine= examin_no ,
                                support= 0,
                                password=args['password'],
                                token = None,
                                last_login = timestamp(),
                                update_at=timestamp(),
                                create_at=timestamp()
                            )
                        )
                    self.db.session.commit
                    return response(success,None,None)
                except Exception as er:
                    return response(error,er,None)
            else:
                return response(error,"账号已注册",None)
        except Exception as er:
            return response(error,er,None)

